﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using GdHotel.Data;
using GdHotel.Data.Interfaces;
using GdHotel.Data.Repositories;
using UniqueTechnologies.Attributes;
using UniqueTechnologies.Data.Interfaces;

namespace GdHotel.Web.Controllers
{ 
    [LanguageSupport]
    [Authorize(Roles = "Admin")]
    public class ManageSideBlocksCustomPagesController : BaseController
    {
        public ISideBlocksCustomPagesRepository SideBlocksCustomPagesRepository { get; set; }
        public override ILanguagesRepository LanguagesRepository { get; set; }
        public ICustomPagesRepository CustomPagesRepository { get; set; }
        public SideBlocksRepository SideBlocksRepository { get; set; }

        public ManageSideBlocksCustomPagesController(ISideBlocksCustomPagesRepository sideBlocksCustomPagesRepository, ILanguagesRepository languagesRepository, ICustomPagesRepository customPagesRepository, SideBlocksRepository sideBlocksRepository)
        {
            SideBlocksCustomPagesRepository = sideBlocksCustomPagesRepository;
            LanguagesRepository = languagesRepository;
            CustomPagesRepository = customPagesRepository;
            SideBlocksRepository = sideBlocksRepository;
        }

        //
        // GET: /ManageSideBlocksCustomPages/

        public ViewResult Index()
        {
            var sideBlocksCustomPages = SideBlocksCustomPagesRepository.FindAllByLang(Language);
            return View(sideBlocksCustomPages);
        }

        //
        // GET: /ManageSideBlocksCustomPages/Details/5

        public ViewResult Details(int id, Int32 sideBlockId)
        {
            var item = SideBlocksCustomPagesRepository.GetById(id);
            ViewBag.ReturnSideBlockId = sideBlockId;
            return View(item);
        }

        //
        // GET: /ManageSideBlocksCustomPages/Create

        public ActionResult Create(Int32? sideBlockId)
        {
            ViewBag.CustomPageId = new SelectList(CustomPagesRepository.FindAllByLang(Language), "Id", "Key");

            var id = sideBlockId ?? -1;
            var sideBlock = SideBlocksRepository.FindAllByLang(Language).FirstOrDefault(block => block.Id == id);
            if (sideBlock == null)
                ViewBag.SideBlockId = new SelectList(SideBlocksRepository.FindAllByLang(Language), "Id", "Title");
            else
                ViewBag.SideBlockId = new SelectList(SideBlocksRepository.FindAllByLang(Language), "Id", "Title", sideBlock.Id);
            ViewBag.ReturnSideBlockId = sideBlockId;
            return View();
        } 

        //
        // POST: /ManageSideBlocksCustomPages/Create

        [HttpPost]
        public ActionResult Create(SideBlockCustomPage item, Int32 sideBlockId)
        {
            if (ModelState.IsValid)
            {
                item.LanguageId = Language.Id;
                SideBlocksCustomPagesRepository.Insert(item);
                SideBlocksCustomPagesRepository.Save();
                return RedirectToAction("Edit", "ManageSideBlocks", new { id = sideBlockId });
            }

            ViewBag.CustomPageId = new SelectList(CustomPagesRepository.FindAllByLang(Language), "Id", "Key");
            ViewBag.SideBlockId = new SelectList(SideBlocksRepository.FindAllByLang(Language), "Id", "Title");
            ViewBag.ReturnSideBlockId = sideBlockId;
            return View(item);
        }
        
        //
        // GET: /ManageSideBlocksCustomPages/Edit/5
 
        public ActionResult Edit(int id, Int32 sideBlockId)
        {
            var item = SideBlocksCustomPagesRepository.GetById(id);
            ViewBag.CustomPageId = new SelectList(CustomPagesRepository.FindAllByLang(Language), "Id", "Key", item.CustomPageId);
            ViewBag.SideBlockId = new SelectList(SideBlocksRepository.FindAllByLang(Language), "Id", "Title", item.SideBlockId);
            ViewBag.ReturnSideBlockId = sideBlockId;
            return View(item);
        }

        //
        // POST: /ManageSideBlocksCustomPages/Edit/5

        [HttpPost]
        public ActionResult Edit(SideBlockCustomPage item, Int32 sideBlockId)
        {
            var dbSideBlockCustomPage = SideBlocksCustomPagesRepository.GetById(item.Id);
            if (ModelState.IsValid)
            {
                UpdateModel(dbSideBlockCustomPage);
                SideBlocksCustomPagesRepository.Save();
                return RedirectToAction("Edit", "ManageSideBlocks", new { id = sideBlockId });
            }
            ViewBag.CustomPageId = new SelectList(CustomPagesRepository.FindAllByLang(Language), "Id", "Key", item.CustomPageId);
            ViewBag.SideBlockId = new SelectList(SideBlocksRepository.FindAllByLang(Language), "Id", "Title", item.SideBlockId);
            ViewBag.ReturnSideBlockId = sideBlockId;
            return View(item);
        }

        //
        // GET: /ManageSideBlocksCustomPages/Delete/5
 
        public ActionResult Delete(int id, Int32 sideBlockId)
        {
            var item = SideBlocksCustomPagesRepository.GetById(id);
            ViewBag.ReturnSideBlockId = sideBlockId;
            return View(item);
        }

        //
        // POST: /ManageSideBlocksCustomPages/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id, Int32 sideBlockId)
        {
            var item = SideBlocksCustomPagesRepository.GetById(id);
            SideBlocksCustomPagesRepository.Delete(item);
            SideBlocksCustomPagesRepository.Save();
            return RedirectToAction("Edit", "ManageSideBlocks", new { id = sideBlockId });
        }

        protected override void Dispose(bool disposing)
        {
            SideBlocksCustomPagesRepository.Dispose();
            SideBlocksRepository.Dispose();
            CustomPagesRepository.Dispose();
            LanguagesRepository.Dispose();
            base.Dispose(disposing);
        }
    }
}